##########################################################
# John Henderson and Alex Theodoridis
# Replication Data for: "Seeing Spots", 
#  Forthcoming in Political Behavior, August 20, 2017
# 
##########################################################
#
#  covar_data.R
#  -- file produces regression covariates from cces_data
#
##########################################################
    
rm(list=ls())                                    
source('~/Dropbox/Seeing_Spots/replication/pre_data.R')

#ymat=cbind(video_skipped, replay, share, getlink, all_y,time_watched, total_time)
  
# goal is to outline characteristics of skippers v. watchers v. other info seekers/avoiders
 # - given characteristics   
 # - given experiment + characteristics

# covariates
pids=pid_lean   
pids[which(pids==-1)]=2 
partisan=abs(pid_lean)
 
libcon=as.numeric(cces_data$ideo5) # lib-con
libcon=libcon-3
libcon[which(libcon>2)]=0

comp=cces_data$comptype # looks like zero...
women=as.numeric(cces_data$gender)-1  # men 0, women 1

educf=cces_data$educ   
educ=as.numeric(cces_data$educ)

age=2012-as.numeric(cces_data$birthyr)

racef=cces_data$race
white=as.numeric(racef=='White')
black=as.numeric(racef=='Black')
hisp=as.numeric(racef=='Hispanic')                               

employf=cces_data$employ
employed=as.numeric(employf=='Full-time')
unemployed=as.numeric(employf=='Unemployed')

marriedf=cces_data$marstat
married=as.numeric(marriedf=='Married')

ownhomef=cces_data$ownhome
ownhome=as.numeric(ownhomef=='Own')

union=as.numeric(as.numeric(cces_data$union)<3)

incomef=cces_data$faminc
income=as.numeric(incomef)
income[which(income>18)]=sample(size=length(which(income>18)),income[which(income<18)])

phonef=cces_data$phone
phone=as.numeric(phonef=='Both' | phonef=='Landline')
                                                 
reg=as.numeric(cces_data$votereg=='Yes')
vote=as.numeric(cces_data$CC316=='Yes. I definitely voted.') # 2008 vote turnout
preschoice=as.numeric(cces_data$CC317=='Barack Obama (Democratic)') # 2008 vote choice
                   
#cces_data$CC354 # 2012 vote turnout vote/intention
#cces_data$CC354b # 2012 vote choice vote
#cces_data$CC354c # 2012 vote choice intention

uncertain=as.numeric(cces_data$CC354c=="I'm not sure")          

statef=cces_data$inputstate 
battleground=as.numeric(
	statef=='Colorado' |
	statef=='Florida'  |
	statef=='Iowa'     |
	statef=='Michigan' |
	statef=='Nevada'   |	    
	statef=='New Hampshire'  |
	statef=='North Carolina' |     
	statef=='Ohio'         |       
	statef=='Pennsylvania' |
	statef=='Virginia'     |
	statef=='Wisconsin')
                                                                    
churchattend=-as.numeric(cces_data$pew_churatd)+abs(min(-as.numeric(cces_data$pew_churatd)))
             
econworse=as.numeric(cces_data$CC302)-3 # econ better worse
econworse[which(econworse==3)]=0

iraq=as.numeric(cces_data$CC305=='Yes') # iraq mistake
#cces_data$CC306 # afghan mistake

approval=as.numeric(cces_data$CC308a)-2.5 # obam approve
approval[which(approval>2)]=0

# party knowledge                
#cces_data$CC309a # party control house
#cces_data$CC309b # party control sen
#cces_data$CC309c # party control state sen
#cces_data$CC309d # party control state assem       
partymajknow=as.numeric(cces_data$CC309a=='Republicans')+as.numeric(cces_data$CC309b=='Democrats')     

# summarized issue attitude score
guncontrol=as.numeric(cces_data$CC320) # gun control 
guncontrol[which(guncontrol==1)]=0
guncontrol[which(guncontrol>2)]=1
guncontrol=guncontrol-1

climate=as.numeric(cces_data$CC321) # global warming
climate[which(climate==6)]=3
climate=climate-3
                
immigration1=as.numeric(cces_data$CC322_1)-1 # immigration  
immigration2=-(as.numeric(cces_data$CC322_2)-2) # immigration  
immigration3=-(as.numeric(cces_data$CC322_3)-2) # immigration  
immigration4=-(as.numeric(cces_data$CC322_4)-2) # immigration  
immigration5=-(as.numeric(cces_data$CC322_5)-2) # immigration  
immigration6=-(as.numeric(cces_data$CC322_6)-2) # immigration  
immigration1[which(immigration1==0)]=-1 
immigration2[which(immigration2==0)]=-1 
immigration3[which(immigration3==0)]=-1 
immigration4[which(immigration4==0)]=-1 
immigration5[which(immigration5==0)]=-1 
immigration6[which(immigration6==0)]=-1 
immigrationscore=rowMeans(cbind(immigration1,immigration2,immigration3,immigration4,immigration5,immigration6))                                          

abortion=-as.numeric(cces_data$CC324)+2.5 # abortion
abortion[which(abortion< -2)]=0

environment=as.numeric(cces_data$CC325)-3 # envir 
environment[which(environment==3)]=0

gaymarriage=as.numeric(cces_data$CC326)-1 # gay marriage
gaymarriage[which(gaymarriage==0)]=-1
gaymarriage[which(gaymarriage==2)]=0

affaction=as.numeric(cces_data$CC327)-2.5 # aff action    
affaction[which(affaction> 2)]=0    
    
govtspending1=as.numeric(cces_data$CC328) # govt spending taxes I
govtspending2=as.numeric(cces_data$CC329) # govt spending taxes II         

d1=taxdefensedomestic = -as.numeric(govtspending1==3 & govtspending2==2)
d2=defensetaxdomestic = -as.numeric(govtspending1==1 & govtspending2==2)   
d3=taxdomesticdefense = -as.numeric(govtspending1==3 & govtspending2==1)   
d4=domestictaxdefense = as.numeric(govtspending1==2 & govtspending2==1)   
d5=defensedomestictax = -as.numeric(govtspending1==1 & govtspending2==3)
d6=domesticdefensetax = as.numeric(govtspending1==2 & govtspending2==3)
d1[which(d1==0)]=1
d2[which(d2==0)]=1
d3[which(d3==0)]=1
d4[which(d4==0)]=-1
d5[which(d5==0)]=1
d6[which(d6==0)]=-1

summary(lm(libcon~d1+d2+d3+d4+d5+d6))
coefs=lm(libcon~d1+d2+d3+d4+d5+d6)$coef[2:7]
d1=d1*coefs[1]
d2=d2*coefs[2]
d3=d3*coefs[3]
d4=d4*coefs[4]
d5=d5*coefs[5]
d6=d6*coefs[6]      
x=rowMeans(cbind(d1,d2,d3,d4,d5,d6))
x=x+abs(min(x))    
x=x/max(x)-.5
x=x/.5             
govtspend=x     

# roll calls
cuts=as.numeric(cces_data$CC332A)-1 # house medicare plan
cuts[which(cuts==0)]=-1
cuts[which(cuts==2)]=0 
cuts=-cuts

bowles=as.numeric(cces_data$CC332B)-1 # simpson bowles budget
bowles[which(bowles==0)]=-1
bowles[which(bowles==2)]=0
bowles=-bowles

demtax=as.numeric(cces_data$CC332C)-1 # bush tax cuts middle dem 
demtax[which(demtax==0)]=-1
demtax[which(demtax==2)]=0

reptax=as.numeric(cces_data$CC332D)-1 # bush tax cuts all rep
reptax[which(reptax==0)]=-1
reptax[which(reptax==2)]=0
reptax=-reptax     

birthcontrol=as.numeric(cces_data$CC332E)-1 # birth control exempt 
birthcontrol[which(birthcontrol==0)]=-1
birthcontrol[which(birthcontrol==2)]=0
birthcontrol=-birthcontrol

korea=as.numeric(cces_data$CC332F)-1 # korea free trade  
korea[which(korea==0)]=-1
korea[which(korea==2)]=0
korea=-korea 

acarepeal=as.numeric(cces_data$CC332G)-1 # repeal ACA 
acarepeal[which(acarepeal==0)]=-1
acarepeal[which(acarepeal==2)]=0  
acarepeal=-acarepeal

keystone=as.numeric(cces_data$CC332H)-1 # keystone pipeline
keystone[which(keystone==0)]=-1
keystone[which(keystone==2)]=0
keystone=-keystone

healthinsurance=as.numeric(cces_data$CC332I)-1 # health insurance 
healthinsurance[which(healthinsurance==0)]=-1
healthinsurance[which(healthinsurance==2)]=0

gaymilitary=as.numeric(cces_data$CC332J)-1 # gay military
gaymilitary[which(gaymilitary==0)]=-1
gaymilitary[which(gaymilitary==2)]=0
          
#govtspend 
xdata=cbind(
	govtspending1,govtspending2,guncontrol,climate,  
	immigration1,immigration2,immigration3, 
	immigration4,immigration5,immigration6,
	abortion,environment,gaymarriage,
	affaction,cuts,bowles,
	demtax,reptax,birthcontrol,
	korea,acarepeal,keystone,
	healthinsurance,gaymilitary,
	pid_lean,libcon#,
	#union,churchattend,income,
	#preschoice,econworse,iraq,approval,	
	#women,married,women*married,
	#educ,age,white,black,hisp
)

d=dist(xdata) 
fit=cmdscale(d,eig=TRUE, k=1) # k is the number of dim
#fit # view results 
#library(MASS)
#fit <- isoMDS(d, k=2)
 
mdsscale=fit$points[,1]  # better measure of libcon than libcon .....
                                                                     
# for consistency use libcon, i.e., for VA sample...
mdsscale=libcon  

# pid strength...   
pid7=as.numeric(cces_data$pid7)-4
pid7[which(pid7>3)]=0                                                                  
  
pid_strenth=abs(pid7)
# covariates in a model: 
#pid_lean,partisan
    
#END covar_data.R